package de.slothsoft.wasbinich.swingclient.control;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import de.slothsoft.commons.swing.CenterLayout;
import de.slothsoft.wasbinich.core.GameResult;
import de.slothsoft.wasbinich.core.Picture;
import de.slothsoft.wasbinich.core.util.PictureUtil;
import de.slothsoft.wasbinich.swingclient.WasBinIchSwingClient;
import de.slothsoft.wasbinich.swingclient.io.ImageUtil;
import de.slothsoft.wasbinich.swingclient.res.Images;
import de.slothsoft.wasbinich.swingclient.res.Messages;

public class AnswerPanel extends JPanel {

    private static final long serialVersionUID = 4175789045266072713L;

    private static final int LABELS_HEIGHT = 100;
    private static final int BORDER = 5;

    private final OnMover onMover;
    private JButton nextButton;
    private JLabel answerLabel;
    private JLabel correctLabel;
    private JLabel alternativesLabel;
    private JLabel sectionLabel;
    private JLabel bigPictureLabel;

    public AnswerPanel(OnMover onMover) {
	this.onMover = onMover;
	setOpaque(false);
	setLayout(new CenterLayout());
	add(createControls());
    }

    private JComponent createControls() {
	JPanel panel = new JPanel();
	panel.setOpaque(false);
	panel.setLayout(new BorderLayout());

	this.answerLabel = createLabel();
	this.answerLabel.setFont(new Font(getFont().getFontName(), Font.BOLD, 16));
	panel.add(this.answerLabel, BorderLayout.NORTH);

	panel.add(createPictureLabels(), BorderLayout.CENTER);
	panel.add(createAnswerLabels(), BorderLayout.SOUTH);

	return panel;
    }

    private Component createPictureLabels() {
	JPanel panel = new JPanel();
	panel.setOpaque(false);
	panel.setLayout(new GridLayout(1, 0));

	this.sectionLabel = createPictureLabel();
	panel.add(this.sectionLabel);
	this.bigPictureLabel = createPictureLabel();
	panel.add(this.bigPictureLabel);

	return panel;
    }

    private Component createAnswerLabels() {
	JPanel panel = new JPanel();
	panel.setOpaque(false);
	panel.setLayout(new BorderLayout());

	this.correctLabel = createLabel();
	this.correctLabel.setFont(new Font(getFont().getFontName(), Font.BOLD, 16));
	panel.add(this.correctLabel, BorderLayout.NORTH);

	this.alternativesLabel = createLabel();
	panel.add(this.alternativesLabel, BorderLayout.CENTER);

	this.nextButton = new WasBinIchButton(Messages.getString("AnswerPanel.next"));
	this.nextButton.addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		performMoveOn();
	    }
	});
	this.nextButton.addKeyListener(new KeyAdapter() {

	    @Override
	    public void keyPressed(KeyEvent e) {
		performMoveOn();
	    }
	});
	panel.add(this.nextButton, BorderLayout.SOUTH);

	return panel;
    }

    protected JLabel createLabel() {
	JLabel result = new JLabel(" ");
	result.setOpaque(false);
	result.setHorizontalAlignment(SwingConstants.CENTER);
	return result;
    }

    private JLabel createPictureLabel() {
	JLabel pictureLabel = new JLabel();
	pictureLabel.setBorder(BorderFactory.createEmptyBorder(BORDER, 0, BORDER, 0));
	return pictureLabel;
    }

    public void calculateAnswer(Picture picture, String answer, ImageIcon questionedImage, GameResult gameResult) {
	String answerLabelText;
	if (PictureUtil.isCorrectAnswer(picture, answer)) {
	    answerLabelText = Messages.getString("AnswerPanel.correct");
	    gameResult.addCorrectAnswer();
	} else {
	    answerLabelText = Messages.getString("AnswerPanel.wrong");
	    gameResult.addWrongAnswer();
	}
	this.answerLabel.setText(answerLabelText);
	this.correctLabel.setText(picture.correctAnswers[0]);
	this.alternativesLabel.setText(PictureUtil.createAlternativeAnswersString(picture.correctAnswers));

	Dimension resizeDimension = getImageResizeDimension();
	this.sectionLabel.setIcon(new ImageIcon(ImageUtil.resize(questionedImage.getImage(), resizeDimension.width,
		resizeDimension.height)));
	if (picture.bigPicture == null || picture.bigPicture.isEmpty()) {
	    this.bigPictureLabel.setIcon(null);
	    this.bigPictureLabel.getParent().setLayout(new CenterLayout());
	} else {
	    this.bigPictureLabel.setIcon(Images.getPictureFromData(picture.bigPicture, resizeDimension));
	    this.bigPictureLabel.getParent().setLayout(new GridLayout(1, 0));
	}
	WasBinIchSwingClient.getMainPanel().doLayout();
	WasBinIchSwingClient.getMainPanel().repaint();
	this.nextButton.requestFocus();
    }

    private Dimension getImageResizeDimension() {
	Dimension resizeDimension = getSize();
	resizeDimension.width = Math.min(resizeDimension.height - LABELS_HEIGHT, resizeDimension.width / 2);
	resizeDimension.height = resizeDimension.width;
	return resizeDimension;
    }

    protected void performMoveOn() {
	this.onMover.moveOn();
    }

    /*
	 * 
	 */

    public static interface OnMover {
	void moveOn();
    }
}
